package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Vote;
import cn.wolfcode.domain.VoteEmployee;
import cn.wolfcode.domain.VoteItem;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IVoteEmployeeService;
import cn.wolfcode.service.IVoteItemService;
import cn.wolfcode.service.IVoteService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.Request;
import cn.wolfcode.util.SessionUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/vote")
public class VoteController {

    @Autowired
    private IVoteService voteService;

    @Autowired
    private IVoteItemService voteItemService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IVoteEmployeeService voteEmployeeService;

    @Request(name="投票页面",expression="vote:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Vote> pageInfo = voteService.query(qo);
        model.addAttribute("pageResult", pageInfo);
        Long id = SessionUtil.getEmployee().getId();
        List<Vote> check = voteService.check(id);
        model.addAttribute("check",check);
        model.addAttribute("id",id);
        return "vote/list";
    }


    @Request(name="投票删除",expression="vote:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           voteService.delete(id);
        }
        return "redirect:/vote/list";
    }


    @Request(name="投票新增/编辑",expression="vote:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Vote vote) {
        if (vote.getId() == null) {
            voteService.save(vote);
        } else {
            voteService.update(vote);
        }
        return "redirect:/vote/list";
    }


    @Request(name="投票新增/编辑",expression="vote:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long voteId, Model model,Long ovteItemId) {
        Employee employee = SessionUtil.getEmployee();
        Long id = employee.getId();

        voteService.inputVoteEmployee(voteId,id,ovteItemId);

        List<Vote> i = voteService.countVoteEmployee(voteId);

        List<VoteEmployee> voteEmployees = voteEmployeeService.listAll(voteId);
        model.addAttribute("voteEmployees",voteEmployees);


        List<Double> a = new ArrayList<>();
        List<Employee> employees = employeeService.selectList();
        System.out.println(i.toString());
        Integer all = employees.size() + 1;
        System.out.println(i.toString()+"===================");
        Double b = 0.0;


        model.addAttribute("a",a);
        System.out.println(a.toString());

        List<VoteItem> voteItems = voteItemService.selectVote(voteId);
        model.addAttribute("voteItems",voteItems);

        Vote vote = voteService.get(voteId);

        model.addAttribute("vote",vote);

        model.addAttribute("count",0);

        model.addAttribute("all",all);
        model.addAttribute("i",i);
        return "vote/end";
    }

    @RequestMapping("/show")
    public String show(Model model,Long voteId){
        Vote vote = voteService.get(voteId);

        model.addAttribute("vote",vote);
        List<VoteItem> voteItems = voteItemService.selectVote(voteId);
        model.addAttribute("voteItems",voteItems);
        return "vote/show";
    }
}
